package com.crm.miaohe.mapper;

import com.crm.miaohe.entity.order.OrderErpEntity;
import com.crm.miaohe.model.TblOrder;
import com.crm.miaohe.model.TblOrderErpExample;
import com.crm.miaohe.model.TblOrderExample;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface TblOrderMapper {
    long countByExample(TblOrderExample example);

    int deleteByExample(TblOrderExample example);

    @Delete({
            "delete from tbl_order",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into tbl_order (order_no, goods_number, ",
            "amount, customer_id, ",
            "erp_order_no, status, ",
            "create_time, create_user, ",
            "update_time, update_user, ",
            "version,",
            "wuliu_code,",
            "fnote)",
            "values (#{orderNo,jdbcType=VARCHAR}, #{goodsNumber,jdbcType=INTEGER}, ",
            "#{amount,jdbcType=DOUBLE}, #{customerId,jdbcType=BIGINT}, ",
            "#{erpOrderNo,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}, ",
            "#{createTime,jdbcType=TIMESTAMP}, #{createUser,jdbcType=VARCHAR}, ",
            "#{updateTime,jdbcType=TIMESTAMP}, #{updateUser,jdbcType=VARCHAR}, ",
            "#{version,jdbcType=BIGINT}, #{wuliu_code,jdbcType=VARCHAR}), ",
            "#{fnote,jdbcType=VARCHAR}, #{fnote,jdbcType=VARCHAR})"
    })
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insert(TblOrder record);

    int insertSelective(TblOrder record);

    List<TblOrder> selectByExample(TblOrderExample example);

    //根据ERP条件来查询订单
    List<OrderErpEntity> selectByErpExample(TblOrderErpExample example);

    //根据ERP条件来查询订单
    List<OrderErpEntity> selectByErpExampleCategory(TblOrderErpExample example, List<String> categoryList);

    @Select({
            "select",
            "tbo.id as tbo_id, tbo.order_no as tbo_order_no, tbo.goods_number as tbo_goods_number, ",
            "tbo.amount as tbo_amount, tbo.customer_id as tbo_customer_id, tbo.erp_order_no as tbo_erp_order_no, ",
            "tbo.status as tbo_status, tbo.create_time as tbo_create_time, tbo.create_user as tbo_create_user, ",
            "tbo.update_time as tbo_update_time, tbo.update_user as tbo_update_user,tbo.wuliu_code as tbo_wuliu_code,tbo.version as tbo_version,tbo.fnote as tbo_fnote,tbo.wuliu_name as tbo_wuliu_name",
            "from tbl_order tbo",
            "where tbo.id = #{id,jdbcType=BIGINT}"
    })
    @ResultMap("com.crm.miaohe.mapper.TblOrderMapper.BaseResultMap")
    TblOrder selectByPrimaryKey(Long id);

    int updateByExampleSelective(@Param("record") TblOrder record, @Param("example") TblOrderExample example);

    int updateByExample(@Param("record") TblOrder record, @Param("example") TblOrderExample example);

    int updateByPrimaryKeySelective(TblOrder record);

    @Update({
            "update tbl_order",
            "set order_no = #{orderNo,jdbcType=VARCHAR},",
            "goods_number = #{goodsNumber,jdbcType=INTEGER},",
            "amount = #{amount,jdbcType=DOUBLE},",
            "customer_id = #{customerId,jdbcType=BIGINT},",
            "erp_order_no = #{erpOrderNo,jdbcType=VARCHAR},",
            "status = #{status,jdbcType=INTEGER},",
            "create_time = #{createTime,jdbcType=TIMESTAMP},",
            "create_user = #{createUser,jdbcType=VARCHAR},",
            "update_time = #{updateTime,jdbcType=TIMESTAMP},",
            "update_user = #{updateUser,jdbcType=VARCHAR},",
            "version = #{version,jdbcType=BIGINT}",
            "wuliu_code = #{wuliu_code,jdbcType=VARCHAR}",
            "fnote = #{fnote,jdbcType=VARCHAR}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(TblOrder record);
}
